package gases.as3.librarys.components.skin
{
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;
	
	import gases.as3.librarys.components.resource.MacroResourceManagaers;
	import gases.as3.librarys.components.resource.net.ResResponse;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.data.ButtonSkin;
	import gases.as3.librarys.components.skin.data.IconCellListSkin;
	import gases.as3.librarys.components.skin.data.ImageSkin;
	import gases.as3.librarys.components.skin.data.PanelSkin;
	import gases.as3.librarys.components.skin.data.ScrollSkin;
	import gases.as3.librarys.components.skin.data.SkinStyleData;
	import gases.as3.librarys.components.skin.data.TextInputSkin;
	import gases.as3.librarys.components.skin.data.TreeSkin;
	import gases.as3.librarys.components.skin.transform.BaseTransform;
	import gases.as3.librarys.core.Interface.IDispose;
	
	/**
	 * 皮肤样式转换器 
	 * @author gases
	 * 
	 */	
	public class SkinTransform extends EventDispatcher implements IDispose
	{
		public function SkinTransform()
		{
			init();
		}
		
		private function init():void
		{
			_skinFactory = new SkinFactory();
			registerSkinClass();
		}
		
		/**
		 * 设置皮肤解析配置实例 
		 * @param config
		 * 
		 */	
		public function setConfigTransform(transform:BaseTransform):void
		{
			_skinTransform = transform;
		}
		
		/**
		 * 解析皮肤配置 
		 * @param config
		 * 
		 */		
		public function resloveSkinConfig(config:Object):void
		{
			if(!_skinTransform)
				return;
			var data:Object = _skinTransform.resloveConfig(config);
			for each(var skinData:Object in data)
			{
				var skin:BaseSkin = getSkin(skinData);
				addSkin(skin);
			}
		}
		
		private function getSkin(data:Object):BaseSkin
		{
			var name:String = data.skinname;
			var type:String = data.type;
			var rect:Rectangle = new Rectangle();
			rect.top = data.scale9_top;
			rect.right = data.scale9_right;
			rect.left = data.scale9_left;
			rect.bottom = data.scale9_bottom;
			var width:Number = data.width;
			var height:Number = data.height;
			
			var skin:BaseSkin = _skinFactory.getSkinClass(type);
			skin.scaleRect = rect;
			skin.width = width;
			skin.height = height;
			skin.skinname = name;
			
			var styles:Array = data.styles as Array;
			if(styles)
			{
				var leng:int = styles.length;
				for(var i:int = 0; i< leng; i++)
				{
					var style:Object = styles[i];
					var property:String = style.stylesName;
					var resName:String = style.resName;
					getSkinResource(resName, skin, property);
				}
			}
			return skin;
		}
		
		private function addSkin(skin:BaseSkin):void
		{
			SkinStylesMangaer.instance.addSkinData(skin);
		}
		
		/**
		 * 获取皮肤资源 
		 * @param resName 资源名字
		 * @param skin 皮肤存储器
		 * @param property 皮肤属性
		 * 
		 */		
		private function getSkinResource(resName:String, skin:BaseSkin, property:String):void
		{
			var resData:ResourceDataBack = new ResourceDataBack();
			resData.skin = skin;
			resData.property = property;
			MacroResourceManagaers.instance.UIResManager.getUISkinRes(resName,skin.scaleRect, new ResResponse(resData.callBack, onError));
		}
		
		/**
		 * 返回错误 
		 * 
		 */		
		private function onError():void
		{
			
		}
		
		protected function registerSkinClass():void
		{
			_skinFactory.addSkinClass(SkinType.BUTTON, ButtonSkin);
			_skinFactory.addSkinClass(SkinType.PANEL, PanelSkin);
			_skinFactory.addSkinClass(SkinType.TREE, TreeSkin);
			_skinFactory.addSkinClass(SkinType.TEXT_INPUT, TextInputSkin);
			_skinFactory.addSkinClass(SkinType.SCROLL, ScrollSkin);
			_skinFactory.addSkinClass(SkinType.ICON_CELL_LIST, IconCellListSkin);
			_skinFactory.addSkinClass(SkinType.IMAGE, ImageSkin);
		}
		
		public function dispose():void
		{
			_skinFactory.dispose();
			_skinTransform = null;
			_skinFactory = null;
		}
		
		private var _skinFactory:SkinFactory;
		private var _skinTransform:BaseTransform;
	}
}


import gases.as3.librarys.components.skin.data.BaseSkin;
import gases.as3.librarys.components.skin.data.SkinStyleData;

/**
 * 回调保存类 
 * @author gases
 * 
 */
class ResourceDataBack
{
	public var skin:BaseSkin;
	public var property:String;
	
	public function ResourceDataBack()
	{
		
	}
	
	public function callBack(data:Object):void
	{
		var style:SkinStyleData = new SkinStyleData();
		style.data = data;
		style.targetProperty = property;
		skin.setStylesProperty(style);
		dispose();
	}
	
	public function dispose():void
	{
		skin = null;
	}
}







